Logical operators in Python are used to combine conditional statements. They return a Boolean value that is either True or False. Python supports three types of logical operators:
Logical AND (and)
This operator returns True if both the operands (i.e., conditions) are True. If one or both operands are False, it returns False.
logical AND operator example in python
x = 5
print(x > 3 and x < 10)
Output
True
Logical OR (or)
This operator returns True if at least one of the operands (i.e., conditions) is True. It returns False only if both operands are False.
logical OR operator example in python
x = 5
print(x > 3 or x > 10)
Output
True
Logical NOT (not)
This operator returns True if the operand (i.e., condition) is False, and it returns False if the operand is True. In other words, it inverts the Boolean value of the operand.
logical NOT operator example in python
x = 5
print(not(x > 3 and x < 10))
Output
False
These operators are used in control flow statements like if, while, etc., to decide what code to execute.